package de.xt.logging.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import de.xt.logging.gui.Popup;

public class CopyTask {
	public static void copyLog(File file, boolean backup, String backupPath, String formating, String pathToStorage, String prefix, boolean ooc, boolean farbcode, boolean loginNachrichten, boolean party, boolean pns, boolean shouts) {
		try {
			FileInputStream inFile = new FileInputStream(file);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inFile));
			String newContent = "";
			if (reader.ready()) {
				String line = reader.readLine();
				while (line != null) {
					newContent = newContent + line + "\n";
					line = reader.readLine();
				}
			}
			inFile.close();
			if (backup && backupPath != null) {
				String[] pieces = file.getName().split(File.pathSeparator);
				String orgFileName = pieces[pieces.length - 1];
				String filename = backupPath + "/" + orgFileName;
				FileWriter fwrite = new FileWriter(filename);
				BufferedWriter out = new BufferedWriter(fwrite);
				out.write(newContent);
				out.close();
			}

			String[] patternsForOoc = new String[3];
			patternsForOoc[0] = "\\(\\(.*\\)\\)";
			patternsForOoc[1] = "\\/\\/";
			patternsForOoc[2] = "\\*\\*.*\\*\\*";
			Filter filter = new Filter();
			if (ooc) {
				try {
					newContent = filter.filterReplace(newContent,
							patternsForOoc);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			String[] patternsForCode = new String[6];
			patternsForCode[0] = "<color=[a-zA-Z0-9#]*>";
			patternsForCode[1] = "</color>";
			patternsForCode[2] = "<i>";
			patternsForCode[3] = "</c>";
			patternsForCode[4] = "</i>";
			patternsForCode[5] = "<c=[a-zA-Z0-9#]*>";
			if (farbcode) {
				try {
					newContent = filter.filterReplace(newContent,
							patternsForCode);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			newContent = newContent.replaceAll("  ", " ");

			String[] lines = newContent.split("\n");
			Vector<String> content = new Vector<String>();
			for (String line : lines) {
				boolean toFiltered = false;

				if (!filter.checkIfLineShouldBeFiltered(line, "\\[[\\w]\\]*") && loginNachrichten) {
					toFiltered = true;
				}
				
				if (filter.checkIfLineShouldBeFiltered(line, "^\\[.*\\] .*:  \\[Gruppe\\]|\\[Party\\]")
						&& party) {
					toFiltered = true;
				}

				if (filter.checkIfLineShouldBeFiltered(line, "^\\[.*\\] .*:  \\[Sprechen\\]|\\[Tell\\]")
						&& pns) {
					toFiltered = true;
				}

				if (filter.checkIfLineShouldBeFiltered(line, "^\\[.*\\] .*:  \\[Ruf\\]|\\[Shout\\]")
						&& shouts) {
					toFiltered = true;
				}

				if (!toFiltered) {
					content.add(line);
				}
			}

			Date dt = new Date();
			SimpleDateFormat df = new SimpleDateFormat(formating);
			String filename = pathToStorage + "/" + prefix
					+ df.format(dt) + ".log";
			FileWriter fwrite = new FileWriter(filename);

			BufferedWriter out = new BufferedWriter(fwrite,8192);
			for (String line : content) {
				out.write(line);
				out.newLine();
				out.flush();
			}
			new Popup("Erfolg!","Log wurde kopiert!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
